package com.meishij.activity;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatDelegate;
import android.view.View;
import android.widget.Button;

import com.meishij.R;
import com.meishij.customerview.CutImageView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Wode_Cut_Image_Activity extends AppCompatActivity implements View.OnClickListener {

    private CutImageView cutImageView;
    private Button enter_photo;
    private Button from_photo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wode_cut_image);
        cutImageView = ((CutImageView) findViewById(R.id.wode_cutimageView));
        enter_photo = ((Button) findViewById(R.id.wode_cut_enter_photo));
        from_photo = ((Button) findViewById(R.id.wode_cut_from_photo));
        enter_photo.setOnClickListener(this);
        from_photo.setOnClickListener(this);
        cutImageView.setImageResource(R.mipmap.addsc_icon_40);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wode_cut_enter_photo:
                Bitmap bitmap = cutImageView.clipBitmap();
                File f = new File(Environment.getExternalStorageDirectory(), "headphoto.png");
                if (f.exists()) {
                    f.delete();
                }
                try {
                    FileOutputStream out = new FileOutputStream(f);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //返回地址
                Intent intent=new Intent();
                intent.putExtra("lujing",f.getAbsolutePath());
                setResult(RESULT_OK,intent);
                finish();
                break;
            case R.id.wode_cut_from_photo:
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, 100);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            cutImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }

    }
}
